package com.longg.service.impl;

import com.longg.dao.UserDao;
import com.longg.dao.UserInfoDao;
import com.longg.model.User;
import com.longg.model.UserInfo;
import com.longg.service.IUserInfoService;

import java.util.Date;
import java.util.List;

public class UserInfoServiceImpl implements IUserInfoService {
    private UserInfoDao userInfoDao = new UserInfoDao();
    private UserDao userDao = new UserDao();

    @Override
    public void updateLoginTime(User user) {
        UserInfo userInfo = userInfoDao.findById(user.getId());
        userInfo.setLoginTime(new Date());
        int update = userInfoDao.update(userInfo);
        System.out.println(update);
    }

    @Override
    public boolean updateInfo(int id, String realName, String phone, String age, String gender) {
        UserInfo userInfo = userInfoDao.findById(id);
        userInfo.setRealName(realName);
        userInfo.setPhone(phone);
        userInfo.setAge(Integer.parseInt(age));
        userInfo.setGender(gender);
        int update = userInfoDao.update(userInfo);
        return update > 0;
    }

    @Override
    public boolean register(String username, Date registerTime) {
        List<User> userList = userDao.findByCol("username", username);
        if(userList.isEmpty()){
            return false;
        }
        UserInfo userInfo = new UserInfo();
        userInfo.setUserId(userList.get(0).getId());
        userInfo.setRegisterTime(registerTime);
        int save = userInfoDao.save(userInfo);
        return save > 0;
    }

    @Override
    public long getCount(String name) {
        return userInfoDao.getCount(name);
    }

    @Override
    public int updateImg(int id, String path) {
        UserInfo userInfo = userInfoDao.findById(id);
        userInfo.setPic(path);
        int update = userInfoDao.update(userInfo);
        return update;
    }
}
